from re import sub

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
from PyQt5 import QtWidgets, QtCore

from settings.settings import *

def general_callback(item):
    print("general_callback -> " + str(item))
    pass


def createSetting(parent, name, value="", callback=general_callback, subMessage=None):
    setting0 = QWidget(parent)
    setting0.layout = QVBoxLayout(parent)
    setting0.layout.setContentsMargins(0, 0, 0, 0)
    setting0.setLayout(setting0.layout)

    setting = QWidget(setting0)
    setting.layout = QHBoxLayout(parent)
    setting.layout.setContentsMargins(0, 0, 0, 0)
    setting.setLayout(setting.layout)

    label = QLabel()
    label.setText(name)
    setting.layout.addWidget(label)

    if isinstance(value, bool):
        v = QCheckBox()
        v.setChecked(value)
        v.stateChanged.connect(callback)
        setting.layout.addWidget(v)
    elif isinstance(value, list):
        v = QComboBox()
        v.addItems(value)
        v.activated.connect(callback)
        setting.layout.addWidget(v)
    else:
        v = QLineEdit()
        v.setText(value)
        v.textEdited.connect(callback)
        setting.layout.addWidget(v)

    setting0.layout.addWidget(setting)
    if subMessage is not None:
        l = QLabel()
        l.setText(subMessage)
        setting0.layout.addWidget(l)

    return setting0

class HomeTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(120, 80, 0, 0)
        self.layout.setAlignment(Qt.AlignLeft)
        self.layout.setSpacing(0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.lblFont = QFont()
        self.lblFont.setPointSize(32)
        self.lbl = QLabel()
        self.lbl.setText("yawk.")
        self.lbl.setFont(self.lblFont)
        self.lbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.lbl, Qt.AlignTop)

        self.slbl = QLabel()
        self.slbl.setText("Yet Another Webkit Browser.")
        self.lblFont2 = QFont()
        self.lblFont2.setPointSize(18)
        self.slbl.setFont(self.lblFont2)
        self.slbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.slbl, Qt.AlignTop)

        self.verlbl = QLabel()
        self.verlbl.setText("release 0.0.5, 24/07/2021")
        self.verlbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.verlbl, Qt.AlignTop)

        self.spacer = QSpacerItem(0, 3820, QSizePolicy.Ignored, QSizePolicy.Maximum)
        self.layout.addSpacerItem(self.spacer)

class BrowsingTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(120, 80, 300, 0)
        self.layout.setAlignment(Qt.AlignLeft)
        self.layout.setSpacing(0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.lblFont = QFont()
        self.lblFont.setPointSize(32)
        self.lbl = QLabel()
        self.lbl.setText("Browsing.")
        self.lbl.setFont(self.lblFont)
        self.lbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.lbl, Qt.AlignTop)

        self.slbl = QLabel()
        self.slbl.setText("16GB of RAM is required.")
        self.lblFont2 = QFont()
        self.lblFont2.setPointSize(16)
        self.slbl.setFont(self.lblFont2)
        self.slbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.slbl, Qt.AlignTop)

        self.spacer1 = QSpacerItem(0, 60, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer1)

        try:
            self.layout.addWidget(createSetting(self, "Home Page", HOME_PAGE), Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer2 = QSpacerItem(0, 20, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer2)

        def browser_mode_handle(value):
            writeSetting("sys.browser_mode", value)
            pass

        try:
            self.layout.addWidget(createSetting(self, "Browser Mode", ["WebEngine (Chromium, default)", "WebKit (Qt)"], callback=browser_mode_handle), Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer2 = QSpacerItem(0, 20, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer2)

        def prefetch_handle(value):
            v = value == 2
            print("prefetch: " + str(v))
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Enable Prefetching", getSettingAsBool("newtork.prefetching", False), callback=prefetch_handle,
                subMessage="This improves loading times at the cost of privacy.\nThis is disabled on Safe + Tor Mode tabs."),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer = QSpacerItem(0, 3820, QSizePolicy.Ignored, QSizePolicy.Maximum)
        self.layout.addSpacerItem(self.spacer)

class NetworkingTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(120, 80, 300, 0)
        self.layout.setAlignment(Qt.AlignLeft)
        self.layout.setSpacing(0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.lblFont = QFont()
        self.lblFont.setPointSize(32)
        self.lbl = QLabel()
        self.lbl.setText("Networking.")
        self.lbl.setFont(self.lblFont)
        self.lbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.lbl, Qt.AlignTop)

        self.slbl = QLabel()
        self.slbl.setText("Connection timed out.")
        self.lblFont2 = QFont()
        self.lblFont2.setPointSize(16)
        self.slbl.setFont(self.lblFont2)
        self.slbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.slbl, Qt.AlignTop)

        self.spacer = QSpacerItem(0, 3820, QSizePolicy.Ignored, QSizePolicy.Maximum)
        self.layout.addSpacerItem(self.spacer)

class PrivacyTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(120, 80, 300, 0)
        self.layout.setAlignment(Qt.AlignLeft)
        self.layout.setSpacing(0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.lblFont = QFont()
        self.lblFont.setPointSize(32)
        self.lbl = QLabel()
        self.lbl.setText("Privacy.")
        self.lbl.setFont(self.lblFont)
        self.lbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.lbl, Qt.AlignTop)

        self.slbl = QLabel()
        self.slbl.setText(";)")
        self.lblFont2 = QFont()
        self.lblFont2.setPointSize(16)
        self.slbl.setFont(self.lblFont2)
        self.slbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.slbl, Qt.AlignTop)

        self.spacer1 = QSpacerItem(0, 40, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer1)

        self.spacer2 = QSpacerItem(0, 20, QSizePolicy.Ignored, QSizePolicy.Fixed)

        def webrtc_handle(value):
            v = value == 2
            writeSetting("network.cullwebrtc", v)
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Restrict WebRTC", getSettingAsBool("network.cullwebrtc", False), callback=webrtc_handle,
                subMessage="Prevents WebRTC from discovering your true IP address.\nThis is enabled on Safe + Tor Mode tabs."),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.layout.addSpacerItem(self.spacer2)

        def webrtc_disable_handle(value):
            v = value == 2
            writeSetting("network.disablewebrtc", v)
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Disable WebRTC", getSettingAsBool("network.disablewebrtc", False), callback=webrtc_disable_handle,
                subMessage="Prevents WebRTC from running.\nThis is enabled on Safe + Tor Mode tabs."),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.layout.addSpacerItem(self.spacer2)

        def keyboard_paste_handle(value):
            v = value == 2
            writeSetting("js.enable_clipboard_access", v)
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Enable Clipboard Access", getSettingAsBool("js.enable_clipboard_access", False), callback=keyboard_paste_handle,
                subMessage="Allows website to automatically paste from your clipboard.\nThis is disabled on Safe + Tor Mode tabs."),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer = QSpacerItem(0, 3820, QSizePolicy.Ignored, QSizePolicy.Maximum)
        self.layout.addSpacerItem(self.spacer)

class AdvancedTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(120, 80, 300, 0)
        self.layout.setAlignment(Qt.AlignLeft)
        self.layout.setSpacing(0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.lblFont = QFont()
        self.lblFont.setPointSize(32)
        self.lbl = QLabel()
        self.lbl.setText("Advanced.")
        self.lbl.setFont(self.lblFont)
        self.lbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.lbl, Qt.AlignTop)

        self.slbl = QLabel()
        self.slbl.setText("Powered by SIGSEGV!")
        self.lblFont2 = QFont()
        self.lblFont2.setPointSize(16)
        self.slbl.setFont(self.lblFont2)
        self.slbl.setFrameStyle(QFrame.NoFrame)
        self.layout.addWidget(self.slbl, Qt.AlignTop)

        self.spacer1 = QSpacerItem(0, 40, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer1)

        def hw_accel_handle(value):
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Use Hardware Acceleration", getSettingAsBool("sys.hw_accel", True), callback=hw_accel_handle,
                subMessage="Improves performance by offloading work to the GPU."),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer2 = QSpacerItem(0, 20, QSizePolicy.Ignored, QSizePolicy.Fixed)
        self.layout.addSpacerItem(self.spacer2)

        def tab_suspend_handle(value):
            writeSettingAsBool("sys.tab_suspend", value)
            pass

        try:
            self.layout.addWidget(createSetting(
                self, "Enable Tab Suspension", getSettingAsBool("sys.tab_suspend", False), callback=tab_suspend_handle,
                subMessage="Freezes inactive tabs to save power and CPU cycles. (WebEngine ONLY)"),
                                  Qt.AlignTop)
        except Exception as e:
            print(e)

        self.spacer = QSpacerItem(0, 3820, QSizePolicy.Ignored, QSizePolicy.Maximum)
        self.layout.addSpacerItem(self.spacer)

def encapulsateWidget(parent, widget):
    qw = QWidget(parent)
    qw.layout = QVBoxLayout(parent)
    qw.layout.setContentsMargins(0, 0, 0, 0)
    qw.setContentsMargins(0, 0, 0, 0)
    qw.setLayout(qw.layout)

    qsa = QScrollArea(parent)
    qsa.setWidget(widget)
    qsa.setMaximumSize(1920, 1080)
    qw.layout.addWidget(qsa)

    return qw

class SettingsTab(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QHBoxLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.tabs = QTabWidget()
        self.layout.addWidget(self.tabs)

        try:
            self.tabs.addTab(HomeTab(self), "Home.")
        except Exception as e:
            print(e)
        self.tabs.addTab(encapulsateWidget(self, BrowsingTab(self)), "Browsing.")
        self.tabs.addTab(encapulsateWidget(self, NetworkingTab(self)), "Networking.")
        self.tabs.addTab(encapulsateWidget(self, PrivacyTab(self)), "Privacy.")
        self.tabs.addTab(encapulsateWidget(self, AdvancedTab(self)), "Advanced.")
        self.tabs.addTab(encapulsateWidget(self, QtWidgets.QWidget()), "fuckAdvertising.")
